<script>
//app.js
import apimanager from './utils/apimanager.js';

const commonLoginUrl = require('config').commonLoginUrl;

const checkUrl = require('config').checkUrl;

const loginmanager = require('utils/loginManager');

export default {
    data() {
        return {};
    },
    onLaunch: function () {
        this.globalData.apimanager.getRequest(checkUrl).then((res) => {
            this.globalData.regname = res.regname;
        });
        this.globalData.uid = uni.getStorageSync('uid');
        this.globalData.relogin(true); // 获取用户信息

        uni.getSetting({
            success: (res) => {
                if (res.authSetting['scope.userInfo']) {
                    uni.getUserInfo({
                        success: (res) => {
                            // 可以将 res 发送给后台解码出 unionId
                            this.globalData.userInfo = res.userInfo;
                        }
                    });
                }
            }
        });
    },
    globalData: {
        userInfo: null,
        uid: '',
        formhash: '',
        repliesrank: '',
        regname: '',
        allowpostcomment: [],
        duration: 2000,

        relogin(isInit) {
            // 登录
            uni.login({
                success: (res) => {
                    let dic = {
                        code: res.code
                    };
                    this.apimanager
                        .getRequest(commonLoginUrl, dic)
                        .then((res) => {
                            if (res.Message.messageval == 'login_succeed') {
                                this.uid = res.Variables.member_uid;

                                if (!isInit) {
                                    uni.showToast({
                                        title: '登录成功！',
                                        icon: 'none'
                                    });
                                }
                            } else {
                                if (res.Message.messageval == 'no_bind') {
                                    loginmanager.openid = res.Variables.openid;
                                    loginmanager.unionid = res.Variables.unionid;
                                    loginmanager.toLogin();
                                }
                            }
                        })
                        .catch((res) => {});
                }
            });
        },

        apimanager: new apimanager()
    }
};
</script>
<style>
@import './app.css';
</style>
